package com.stu.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class IndicatorView extends View {
	private Paint textPaint, boxPaintOff, boxPaintOn;
	private Rect boxRect;
	private boolean isOn;
	private String offText, onText;
	private final static int width=100, height=35, textSize=20,
	  textX=50, textY=22;

	public IndicatorView(Context context) {
		super(context);
		init();
	}

	public IndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public IndicatorView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	public void setState( boolean state ) {
		isOn = state;
	}
	
	public boolean getState() {
		return isOn;
	}
	
	public void setText( String off, String on ) {
		offText = off;
		onText = on;
	}
	
	private void init() {
		offText = new String("Text");
		onText = new String();
		isOn = false;
		boxRect = new Rect(0, 0, width, height);

		textPaint = new Paint();
		textPaint.setColor(Color.BLACK);
		textPaint.setTextAlign(Paint.Align.CENTER);
		textPaint.setTextSize(textSize);
		
		boxPaintOn = new Paint();
		boxPaintOn.setColor(Color.GREEN);
		boxPaintOn.setStyle(Paint.Style.FILL);
		
		boxPaintOff = new Paint();
		boxPaintOff.setColor(Color.GRAY);
		boxPaintOff.setStyle(Paint.Style.FILL);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(width, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if( isOn ) {
			canvas.drawRect(boxRect, boxPaintOn);
			canvas.drawText(onText, textX, textY, textPaint);
		} else {
			canvas.drawRect(boxRect, boxPaintOff);
			canvas.drawText(offText, textX, textY, textPaint);
		}
	}

}
